<?php namespace Admin\Controller;
//货品控制器
class GoodsListController extends CommonController{
	private $model;
	public function __auto(){
		$this->model = new \Common\Model\GoodsList;
	}
	
	//货品列表
	public function index(){
		$gid=Q('get.gid',0,'intval');
		//货品添加
		if(IS_POST){
//			p(Q('post.'));exit;
			if($this->model->store()){
				View::success("添加成功");
			}
			View::error($this->model->getError());
		}
//		p($gid);exit;
		//处理表格表头的规格数据  (颜色、内存)
		$goodsAttr=new \Common\Model\GoodsAttr;
		$data=$goodsAttr->join('typeattr','typeattr_taid','=','taid')
						->where("goods_gid={$gid} AND class=1")
						->groupBy('taid')
						->get();
//		p($data);
		//添加货品的时候下拉菜单所选择的内容(商品属性中获得)
		foreach ($data as $k=>$v){
			$data[$k]['value']=$goodsAttr
							->where("typeattr_taid={$v['taid']} AND goods_gid={$gid}")
							->get();
		}
//		p($data);
		View::with('data',$data);
		
		//显示以后的货物
		$goodsList=new \Common\Model\GoodsList;
		$listData=$goodsList->where("goods_gid={$gid}")->get();
//		p($listData);exit;
		foreach ($listData as $k => $v) {
            $listData[$k]['combine'] = explode('|',$v['combine']);
        }
		foreach ($listData as $k=>$v){
			foreach ($v['combine'] as $key=>$value){
				$listData[$k]['combine'][$key]=Db::table('goods_attr')->where('gtid',$value)->pluck('gtvalue');
			}
		}
        View::with('listData',$listData);
		View::make();
	}
	
	//货品编辑
	public function edit(){
		$gid=Q('get.gid',0,'intval');
		//货品添加
		if(IS_POST){
//			p(Q('post.'));exit;
			if($this->model->edit()){
				View::success("修改成功",U('GoodsList/index',array('gid'=>$gid)));
			}
			View::error($this->model->getError());
		}
//		p($gid);exit;
		//处理表格表头的规格数据  (颜色、内存)
		$goodsAttr=new \Common\Model\GoodsAttr;
		$data=$goodsAttr->join('typeattr','typeattr_taid','=','taid')
						->where("goods_gid={$gid} AND class=1")
						->groupBy('taid')
						->get();
//		p($data);
		//添加货品的时候下拉菜单所选择的内容(商品属性中获得)
		foreach ($data as $k=>$v){
			$data[$k]['value']=$goodsAttr
							->where("typeattr_taid={$v['taid']} AND goods_gid={$gid}")
							->get();
		}
//		p($data);
		View::with('data',$data);
		
		//显示以后的货物
		$goodsList=new \Common\Model\GoodsList;
		$listData=$goodsList->where("goods_gid={$gid}")->get();
//		p($listData);exit;
		foreach ($listData as $k => $v) {
            $listData[$k]['combine'] = explode('|',$v['combine']);
        }
		foreach ($listData as $k=>$v){
			foreach ($v['combine'] as $key=>$value){
				$listData[$k]['combine'][$key]=Db::table('goods_attr')->where('gtid',$value)->pluck('gtvalue');
			}
		}
        View::with('listData',$listData);
		
		$glid=Q('get.glid',0,'intval');
		$oldData=$this->model->where("glid={$glid}")->find();
//		p($oldData);exit;
		View::with('oldData',$oldData);
		View::make();
	}

	//货品删除
	public function del(){
		$glid=Q('get.glid',0,'intval');
		$this->model->where("glid={$glid}")->delete();
		View::success("删除成功");
	}
}

